﻿using System;
using ControleRepresentanteSync.Application.Wizard;

namespace ControleRepresentanteSync.Application.DynamicSync
{
    abstract class DynamicSyncContentPresenter<TContentViewModel> : WizardPagePresenterBase<TContentViewModel> 
        where TContentViewModel : IDynamicSyncContentViewModel
    {
        protected DynamicSyncContentPresenter(TContentViewModel viewModel)
            : base(viewModel)
        {
            ViewModel.Changed += ViewModelChanged;
        }

        void ViewModelChanged(object sender, EventArgs e)
        {
            UpdateIsValid();
        }

        protected void UpdateIsValid()
        {
            ViewModel.IsValid = IsValid();
        }

        protected abstract bool IsValid();

        protected sealed override bool TryApply()
        {
            return true;
        }

        public override void Dispose()
        {
            base.Dispose();

            ViewModel.Changed -= ViewModelChanged;
        }
    }
}
